package com.example.robertluozizhao.framecollectdemo.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author luo_zi_zhao
 * @version V1.0 当前版本描述信息
 * @FileName com.example.robertluozizhao.framecollectdemo.util
 * @date 2019/8/19 10:10
 */

public class SDcardUtils {

    private static String TAG = "SDcardUtils";

    // 获取手机外置sd卡路径
    public static String getSDCardPath() {
        String sdPath = "";
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("mount");
            is = proc.getInputStream();
            isr = new InputStreamReader(is);
            String line;
            String mount = new String();
            BufferedReader br = new BufferedReader(isr);
            while ((line = br.readLine()) != null) {
                if (line.contains("secure"))
                    continue;
                if (line.contains("asec"))
                    continue;
                if (line.contains("fat")) {
                    String columns[] = line.split(" ");
                    if (columns != null && columns.length > 1) {
                        mount = mount.concat("*" + columns[1]);
                    }
                } else if (line.contains("fuse")) {
                    String columns[] = line.split(" ");
                    if (columns != null && columns.length > 1) {
                        mount = mount.concat("*" + columns[1]);
                    }
                }
            }
            String[] sdPathArr = mount.split("\\*");
            for (String path : sdPathArr) {

                Log.d(TAG, "getSDCardPath --> path:"+path);


                if (path.contains("usb_storage")) {
                    sdPath = path;
                    break;
                }

/*                if (path.contains("ex") || path.contains("EX") || path.contains("eX") || path.contains("Ex")) {
                    sdPath = path;
                    break;
                }*/
            }
        } catch (Exception e) {
            Log.d(TAG, "getSDCardPath --> error:"+e.getMessage());
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (Exception e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }
        return sdPath;
    }

}
